﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ImageExtension.cs" company="">
//   
// </copyright>
// <summary>
//   The image extension.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Drawing
{
    using System.Drawing;
    using System.Drawing.Imaging;

    /// <summary>
    /// The image extension.
    /// </summary>
    public static class ImageExtension
    {
        #region Public Methods and Operators

        /// <summary>
        /// The save and set extension.
        /// </summary>
        /// <param name="image">
        /// The image.
        /// </param>
        /// <param name="fileNameWithoutExtension">
        /// The file name without extension.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public static string SaveAndSetExtension(this Image image, string fileNameWithoutExtension)
        {
            string path;
            var format = image.RawFormat;
            if (image.RawFormat == ImageFormat.Gif)
            {
                path = fileNameWithoutExtension + "." + ImageFileExtensions.Gif;
            }
            else if (image.RawFormat == ImageFormat.Jpeg)
            {
                path = fileNameWithoutExtension + "." + ImageFileExtensions.Jpg;
            }
            else if (image.RawFormat == ImageFormat.Png)
            {
                path = fileNameWithoutExtension + "." + ImageFileExtensions.Png;
            }
            else
            {
                path = fileNameWithoutExtension + "." + ImageFileExtensions.Png;
                format = ImageFormat.Png;
            }

            image.Save(path, format);
            return path;
        }

        #endregion
    }
}